package com.yunkaweilai.android.activity.recharge;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.model.ShopRechargeInfoBean;
import com.yunkaweilai.android.model.login.IndustryModel;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/2/20.
 */

public class RechargeTimeActivity extends BaseActivity {

    @BindView(R.id.id_listview)
    ListView idListview;

    public static final String RECHARGE_TIME_LIST = "RECHARGE_TIME_LIST";
    public static final String RECHARGE_TYPE = "RECHARGE_TYPE";
    public static final String RECHARGE_KEY = "RECHARGE_KEY";
    public static final String RECHARGE_NAME = "RECHARGE_NAME";
    private List<ShopRechargeInfoBean.DataBean.StoreProductBean> list = new ArrayList<>();
    private String type;// 1 pc  2 app  3pad  4wechat
    private CommonAdapter<ShopRechargeInfoBean.DataBean.StoreProductBean> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recharge_time_layout);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("选择有效期")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        type = getIntent().getStringExtra(RECHARGE_TYPE);
        List<ShopRechargeInfoBean.DataBean.StoreProductBean> activitylist = (List<ShopRechargeInfoBean.DataBean.StoreProductBean>) getIntent().getSerializableExtra(RECHARGE_TIME_LIST);
        ShopRechargeInfoBean.DataBean.StoreProductBean bean = new ShopRechargeInfoBean.DataBean.StoreProductBean();
        bean.setTimeStr("未选择");
        bean.setKey("");
        list.add(bean);
        list.addAll(activitylist);
        initAdapter();
        initClick();
    }

    private void initAdapter() {
        adapter = new CommonAdapter<ShopRechargeInfoBean.DataBean.StoreProductBean>(this,R.layout.item_list_company,list) {
            @Override
            protected void convert(ViewHolder viewHolder, ShopRechargeInfoBean.DataBean.StoreProductBean item, final int position) {
                TextView idTvCompany = viewHolder.getView(R.id.id_tv_company);
//                idTvCompany.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        String price = "";
//                        ShopRechargeInfoBean.DataBean.StoreProductBean item = list.get(position);
//                        switch (type){
//                            case "1":
//                                price = item.getPC()+"";
//                                break;
//                            case "2":
//                                price = item.getAPP() + "";
//                                break;
//                            case "3":
//                                price = item.getPAD()+"";
//                                break;
//                            case "4":
//                                price = item.getWechat() + "";
//                                break;
//                        }
//                        Intent intent = new Intent();
//                        intent.putExtra(RECHARGE_KEY,list.get(position).getKey()+"");
//                        if (position == 0){
//                            intent.putExtra(RECHARGE_NAME,list.get(position).getTimeStr());
//                        }else{
//                            intent.putExtra(RECHARGE_NAME,list.get(position).getTimeStr()+" / " + Utils.ManageMoney(price));
//                        }
//                        setResult(RESULT_OK, intent);
//                        finish();
//                    }
//                });
                String price = "";
                switch (type){
                    case "1":
                        price = item.getPC()+"";
                        break;
                    case "2":
                        price = item.getAPP() + "";
                        break;
                    case "3":
                        price = item.getPAD()+"";
                        break;
                    case "4":
                        price = item.getWechat() + "";
                        break;
                }
                if (position == 0){
                    viewHolder.setText(R.id.id_tv_company,item.getTimeStr());
                }else{
                    viewHolder.setText(R.id.id_tv_company,item.getTimeStr()+" / " + Utils.ManageMoney(price));
                }
            }
        };
        idListview.setAdapter(adapter);
    }

    private void initClick() {
        idListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String price = "";
                    ShopRechargeInfoBean.DataBean.StoreProductBean item = list.get(position);
                    switch (type){
                        case "1":
                            price = item.getPC()+"";
                            break;
                        case "2":
                            price = item.getAPP() + "";
                            break;
                        case "3":
                            price = item.getPAD()+"";
                            break;
                        case "4":
                            price = item.getWechat() + "";
                            break;
                    }
                    Intent intent = new Intent();
                    intent.putExtra(RECHARGE_KEY,list.get(position).getKey()+"");
                    if (position == 0){
                        intent.putExtra(RECHARGE_NAME,list.get(position).getTimeStr());
                    }else{
                        intent.putExtra(RECHARGE_NAME,list.get(position).getTimeStr()+" / " + Utils.ManageMoney(price));
                    }
                    setResult(RESULT_OK, intent);
                    finish();
            }
        });
    }
}
